﻿Shader "Unlit/EdgeDetectionV2"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _EdgeOnly("Edge Only（仅有边缘颜色，其他为背景色）",Float) = 1.0
        _EdgeColor("Edge Color（边缘颜色）",Color) = (0,0,0,1)
        _BackgroundColor("BackgroundColor（背景颜色）",Color) = (1,1,1,1)
        _SampleDis("SampleDis（采样距离（描边宽度））",Float) = 1.0
        _Sensitivity("Sensitivity（敏感度：x:法线灵敏度；y:深度灵敏度）",Vector) = (1,1,1,1)
    }
    SubShader
    {
        CGINCLUDE
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            half4 _MainTex_TexelSize;
            sampler2D _CameraDepthNormalsTexture; //Unity传递的 深度、法线纹理
            fixed _EdgeOnly;
            fixed4 _EdgeColor;
            fixed4 _BackgroundColor;
            float _SampleDis;
            float4 _Sensitivity;

            struct a2v
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv[5] : TEXCOORD0;
            };
            v2f vert (a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                half2 uv = v.texcoord;
                o.uv[0] = uv;

                #if UNITY_UV_STARTS_AT_TOP //平台差异处理
                if(_MainTex_TexelSize.y<0)
                    uv.y = 1 - uv.y;
                #endif

                //Roberts算子的 纹理采样坐标
                o.uv[1] = uv + _MainTex_TexelSize.xy * half2(1,1) * _SampleDis;
                o.uv[2] = uv + _MainTex_TexelSize.xy * half2(-1,-1) * _SampleDis;
                o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1,1) * _SampleDis;
                o.uv[4] = uv + _MainTex_TexelSize.xy * half2(1,-1) * _SampleDis;

                return o;
            }
            //检测是否有边界
            half CheckSame(half4 center,half4 sample) 
            {
                half2 centerNormal = center.xy; //非真正的法线值
                float centerDepth = DecodeFloatRG(center.zw);
                half2 sampleNormal = sample.xy;
                float sampleDepth = DecodeFloatRG(sample.zw);

                //法线差异：
                half2 diffNormal = abs(centerNormal - sampleNormal) * _Sensitivity.x;
                int isSameNormal = (diffNormal.x + diffNormal.y) < 0.1; //1：相同法线值

                //深度值差异：
                half2 diffDepth = abs(centerDepth - sampleDepth) * _Sensitivity.y;
                int isSameDepth = (diffDepth.x + diffDepth.y) < 0.1 * centerDepth; //1：相同深度值

                return isSameNormal * isSameDepth ? 1.0 : 0.0 ;//1：无明显差异，无边界
            }
            //使用Roberts算子进行边缘检测
            fixed4 fragRobertsCrossDepthAndNormal(v2f i) : SV_Target
            {
                //四次采样
                half4 sample1 = tex2D(_CameraDepthNormalsTexture,i.uv[1]);
                half4 sample2 = tex2D(_CameraDepthNormalsTexture,i.uv[2]);
                half4 sample3 = tex2D(_CameraDepthNormalsTexture,i.uv[3]);
                half4 sample4 = tex2D(_CameraDepthNormalsTexture,i.uv[4]);

                //计算对角线差异
                half edge = 1.0;
                edge *= CheckSame(sample1,sample2);
                edge *= CheckSame(sample3,sample4);

                fixed4 srcCol = lerp(_EdgeColor , tex2D(_MainTex , i.uv[0]) , edge);
                fixed4 edgeCol = lerp(_EdgeColor , _BackgroundColor , edge);

                return lerp(srcCol , edgeCol , _EdgeOnly);
            }
        ENDCG

        ZTest Always Cull Off ZWrite Off
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment fragRobertsCrossDepthAndNormal
            ENDCG
        }
    } 
    Fallback Off
}